import store from '../../store/index'
import {
  acceptDatePatApply,
  refuseDatePatApply,
  getSendDatePatApply,
  getReceiveDatePatApply
} from '../../api/publishDatePat'

import {
  getReceiveActivityApply,
  getSendActivityAppy,
  refuseActivityAppy,
  acceptActivityAppy
} from '../../api/activity'

Page(
  store.createPage({
    data: {
      // 收到约拍详细信息显示 true 表示不显示
      receiveDatePatDetailVasible: true,
      // 发送约拍详细信息显示
      sendDatePatDetailVasible: true,
      // 收到活动申请详细信息显示
      receiveActivityDetailVasible: true,
      // 发送活动申请详细信息显示
      sendActivityDetailVasible: true,

      // 用户收到的约拍申请
      receiveDatePatApply: [],
      // 用户发送的约拍申请
      sendDatePatApply: [],
      // 用户收到的活动申请
      receiveActivityApply: [],
      // 用户发送的活动申请
      sendActivityApply: []
    },
    /**
     * 初始化数据
     * @param {} e
     */
    initData: function(e) {
      const that = this
      // 收到的约拍
      getReceiveDatePatApply().then(function(res) {
        that.setData({
          receiveDatePatApply: res.data
        })
      })
      // 发送的约拍
      getSendDatePatApply().then(function(res) {
        that.setData({
          sendDatePatApply: res.data
        })
      })
      // 收到的活动
      getReceiveActivityApply().then(function(res) {
        that.setData({
          receiveActivityApply: res.data
        })
      })
      // 发送的活动
      getSendActivityAppy().then(function(res) {
        that.setData({
          sendActivityApply: res.data
        })
      })
    },
    /**
     * 显示页面详细信息 dataSet.show
     * @param {*} e
     */
    changeDetailVasible(e) {
      const that = this
      const dataSet = e.currentTarget.dataset
      // 是否显示 true 不现实 false 显示
      const isShow = dataSet.show
      // 哪个 vasible(receiveDatePat, sendDatePat, receiveActivity, sendActivity)
      const vasible = dataSet.vasible

      if (vasible === 'receiveDatePat') {
        that.setData({
          receiveDatePatDetailVasible: isShow
        })
      } else if (vasible === 'sendDatePat') {
        that.setData({
          sendDatePatDetailVasible: isShow
        })
      } else if (vasible === 'receiveActivity') {
        that.setData({
          receiveActivityDetailVasible: isShow
        })
      } else if (vasible === 'sendActivity') {
        that.setData({
          sendActivityDetailVasible: isShow
        })
      }
    },
    /**
     * 接受或者拒绝回调函数
     * dataset.isAccept
     * dataset.applyId
     * @param {} e
     */
    doAcceptOrRefuse: function(e) {
      const that = this
      const isAccept = e.currentTarget.dataset.isAccept
      const applyId = e.currentTarget.dataset.applyId
      if (isAccept) {
        acceptDatePatApply(applyId).then(function(res) {
          that.initData()
        })
      } else {
        refuseDatePatApply(applyId).then(function(res) {
          that.initData()
        })
      }
    },
    doActivityApplyAcceptOrRefuse: function(e) {
      const that = this
      const isAccept = e.currentTarget.dataset.isAccept
      const applyId = e.currentTarget.dataset.applyId
      if (isAccept) {
        acceptActivityAppy(applyId).then(function(res) {
          that.initData()
        })
      } else {
        refuseActivityAppy(applyId).then(function(res) {
          that.initData()
        })
      }
    },
    onLoad: function(params) {
      this.initData()
    },
    onShow: function(params) {
      this.initData()
    }
  })
)
